import http from '@/utils/request'
import type { PageForm, ListResponse, UserInfoType } from './interface'
import { baseUrl } from '@/common/config'
import { useUserStore } from '@/stores'

// 登录
export const _API_Login = (code: string) => {
  return http<{ access_token: string }>({
    url: '/user/login',
    method: 'POST',
    data: { code }
  })
}

// 获取用户信息
export const _API_GetInfo = () => {
  return http<UserInfoType>({
    url: '/user',
    method: 'GET'
  })
}

// 上传图片
export const _API_UploadImage = (filePath: string) => {
  return new Promise<string>((resolve, reject) => {
    uni.uploadFile({
      url: `${baseUrl()}/common/upload/image`,
      filePath,
      name: 'file',
      header: {
        Authorization: `Bearer ${useUserStore().token}`
      },
      success: (res) => {
        return resolve(JSON.parse(res.data).data.url)
      },
      fail: () => {
        reject()
      }
    })
  })
}
